# Contributor: Christoph Reiter <reiter.chrsitoph@gmail.com>

_realname=matplotlib
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.2.2
pkgrel=1
pkgdesc="A python plotting library, making publication quality plots (mingw-w64)"
arch=('any')
url='https://matplotlib.org/'
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-python-pytz"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-python-cycler"
         "${MINGW_PACKAGE_PREFIX}-python-dateutil"
         "${MINGW_PACKAGE_PREFIX}-python-pyparsing"
         "${MINGW_PACKAGE_PREFIX}-python-kiwisolver"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libpng")
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-pyqt5: Qt5Agg/Qt5Cairo backend"
            "${MINGW_PACKAGE_PREFIX}-gtk3: GTK3Agg/GTK3Cairo backend"
            "${MINGW_PACKAGE_PREFIX}-python-gobject: GTK3Agg/GTK3Cairo backend"
            "${MINGW_PACKAGE_PREFIX}-python-cairo: cairo/GTK3Cairo/Qt5Cairo backend"
            "${MINGW_PACKAGE_PREFIX}-python-tornado: WebAgg backend")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/matplotlib/matplotlib/archive/v${pkgver}.tar.gz"
        "https://jqueryui.com/resources/download/jquery-ui-1.12.1.zip"
        setupext.py.patch)
sha256sums=('dd4ff3e19f507a5a07612fa642e6e66fadbaf303483a43dc3acb04067f7ac522'
            'f8233674366ab36b2c34c577ec77a3d70cac75d2e387d8587f3836345c0f624d'
            'a2f0e6b9f7e40881cb1b83025f8797e1431d97068189c2a0468bb84f7ce28945')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/setupext.py.patch

  cd ${srcdir}
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"

  local pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  local query_dest="${pkgdir}${MINGW_PREFIX}/lib/python${pyver}/site-packages/matplotlib/backends/web_backend"
  mkdir -p "${query_dest}"
  cp -R "${srcdir}/jquery-ui-1.12.1" "${query_dest}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
   ${MINGW_PREFIX}/bin/python setup.py install -O1 --skip-build \
   --root="${pkgdir}" --prefix=${MINGW_PREFIX}

  install -Dm644 doc/users/license.rst "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-matplotlib/LICENSE
}
